import React, { useEffect, useState } from "react";
import '../../appcommon.css';
import styles from './passwordchanged.module.css';
import { useDispatch } from "react-redux";
import { DotLoading, Mask, Toast } from "antd-mobile";
import { saveEvent } from "@/store/actions/saveEvent";
import { useHistory } from "react-router-dom/cjs/react-router-dom.min";
import ZjcUtil from "@/utils/zjcUtil";
import { LangThai } from '@/utils/langThai';
import NavigateBar from "@/components/NavigateBar";

/**
 * 修改密码-输入zjc页
 */
export default function PasswordZjcInput() {

    //loading状态
    const [maskVisible, setMaskVisible] = useState(false)
    //显示add home弹框
    const [addHomeMaskVisible, setAddHomeMaskVisible] = useState(false);
    const [addHomeMaskIsHomeMode, setAddHomeMaskIsHomeMode] = useState(true);

    // zjc 输入内容
    const [zjcValue, setZjcValue] = useState('')

    // 文案翻译
    const [curLang, setCurLang] = useState(LangThai);

    // 获取 Redux 分发器
    const dispatch = useDispatch()

    //router
    const history = useHistory()

    //清空登录信息
    useEffect(() => {
        //关闭loading
        setMaskVisible(false)

        //打点
        doHitEvent("PASSWORD_ZJC_INPUT_START")

        return () => {
            //打点
            doHitEvent("PASSWORD_ZJC_INPUT_END")
        }
    }, [])

    // 点击提交
    const onHandleSubmit = () => {
        console.log("点击提交");

        // 判断是否选全对
        if (zjcValue.length > 0) {
            // 缓存
            ZjcUtil.zjc = zjcValue;

            // 输入新密码
            history.push('/passwordnewinput');

        } else {
            Toast.show(curLang.zjcVerifyVerificationFailedPleaseTryAgain);
        }
    };

    // 输入内容监听
    const onHandleInputChange = (e) => {
        setZjcValue(e.target.value);
    };

    //打点接口
    const doHitEvent = async (type, remark = "") => {
        console.log("doHitEvent: " + type + " , " + remark);

        try {
            //获取Action
            const action = saveEvent(type, remark)

            //触发接口
            await dispatch(action)

        } catch (e) {
            console.log(e)
        }
    }

    return (
        <div className="pageRoot">
            <NavigateBar theme="white" showLogout={false} onAddFavorite={() => { setAddHomeMaskIsHomeMode(false); setAddHomeMaskVisible(true) }} onAddHome={() => { setAddHomeMaskIsHomeMode(true); setAddHomeMaskVisible(true) }}>{curLang.meChangePassword}</NavigateBar>

            <div className={styles.passwordListRoot}>

                {/* 标题 */}
                <div className={styles.passwordTitleLarge}>{curLang.zjcInputImportWallet}</div>

                {/* zjc 列表 */}
                <div className={styles.passwordVerifyList}>
                    {/* 第1行 */}
                    <div className={styles.passwordListVerifyContent}>
                        <textarea
                            className={styles.passwordListInputContent}
                            value={zjcValue}
                            onChange={onHandleInputChange}
                            placeholder={curLang.zjcInputTips}
                        />
                    </div>
                </div>

                {/* 提交 */}
                <div className="pageBtnSubmit pageMarginTop16px" onClick={onHandleSubmit}>{curLang.zjcInputNext}</div>

                {/* 底部提示 */}

                {/* 底部 margin */}
                <div className="pageBottomEdge"></div>
            </div>
            
            {/* load mask */}
            <Mask visible={maskVisible} opacity={0.1}
                onMaskClick={() => { }}>
                <div className="pageLoadingOverlayContent">
                    <span style={{ fontSize: 24, color: '#3498DB' }} >
                        <DotLoading color="currentColor" />
                    </span>
                </div>
            </Mask>
        </div >
    )
}